package com.sokal.easycare.modules.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.modules.sys.dao.UserDao;
import com.sokal.easycare.modules.sys.dao.UserRealnameAuthDao;
import com.sokal.easycare.modules.sys.entity.MainUser;
import com.sokal.easycare.modules.sys.entity.UserRealnameAuth;

/**
 * 实名认证Service
 * 
 * @author yangmin
 * @version 2017-04-12
 */
@Service
@Transactional(readOnly = true)
public class UserRealnameAuthService extends CrudService<UserRealnameAuthDao, UserRealnameAuth> {

	@Autowired
	private UserDao userDao;
	
	public UserRealnameAuth get(String id) {
		return super.get(id);
	}

	public List<UserRealnameAuth> findList(UserRealnameAuth userRealnameAuth) {
		return super.findList(userRealnameAuth);
	}

	public Page<UserRealnameAuth> findPage(Page<UserRealnameAuth> page, UserRealnameAuth userRealnameAuth) {
		return super.findPage(page, userRealnameAuth);
	}

	@Transactional(readOnly = false)
	public void save(UserRealnameAuth userRealnameAuth) {
		audit(userRealnameAuth.getId(),userRealnameAuth.getState());
	}

	@Transactional(readOnly = false)
	public void audit(String id, String state) {
		if (StringUtils.isEmpty(id) || StringUtils.isEmpty(state)) {
			throw new IllegalArgumentException("参数错误");
		}
		UserRealnameAuth userRealnameAuth = this.get(id);
		userRealnameAuth.setState(state);
		super.save(userRealnameAuth);
		MainUser user = userRealnameAuth.getUser();
		if ("1".equals(state)) {//审核成功
			user.setStatus("2");
			userDao.updateStatus(user);
		}else if("2".equals(state)) { //审核失败
			user.setStatus("3");
			userDao.updateStatus(user);
		}
	}

	@Transactional(readOnly = false)
	public void delete(UserRealnameAuth userRealnameAuth) {
		super.delete(userRealnameAuth);
	}

}